<template>
  <div>
    <h3>商品详情搜索页面</h3>
    <!-- 搜索框 -->
    <el-row>
      <el-col :span="24">
        <el-autocomplete
          class="keyword"
          v-model="keyword"
          :fetch-suggestions="querySearchAsync"
          placeholder="请输入商品关键字"
          @select="handleSelect"
        ></el-autocomplete>
        <el-button type="primary" @click="searchkeyword">搜索</el-button>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="4">
        价格区间
        <el-input v-model="searchParams.lowPrice" placeholder style="width:100px"></el-input>-
        <el-input v-model="searchParams.highPrice" placeholder style="width:100px"></el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" @click="search">搜索</el-button>
      </el-col>
    </el-row>
    <!--搜索结果  -->
    <div class="clearfix">
      <div v-for="item in  goodList" :key="item.goodIdi">
        <el-card :body-style="{ padding: '0px'   }" class="card">
          <img :src="item.goodImgUrl" alt="图片丢失了" class="goodImg" />
          <div style="padding: 14px;">
            <span v-html="item.goodName"></span>
            <div class="bottom clearfix">
              <div>销量:{{item.goodSell}}</div>
              <div>原价:{{item.goodPrice}}</div>
              <div>现价:{{item.goodNowPrice}}</div>
              <el-button type="text" class="button" @click="gotoGoodDetail(item.goodId)">购买</el-button>
            </div>
          </div>
        </el-card>
      </div>
    </div>

    <!--分页组件  -->
    <div class="block">
      <span class="demonstration">完整功能</span>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchParams.currentPage"
        :page-sizes="[1, 2, 30, 40]"
        :page-size="searchParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { searchGoodDetail, searchGood } from "../api/requestapi";
export default {
  name: "SearchDetail",

  data() {
    return {
      //查询参数
      searchParams: {
        //暂存关键词
        keyword: "",
        //当前页数
        currentPage: 1,
        //页码大小
        pageSize: 10,
        // 价格区间
        lowPrice: "",
        highPrice: "",
      },
      //相关品牌：
      brand: [],
      //搜索结果
      goodList: [],

      //条件总数
      total: 0,

      //当前的关键词用于不相关商品跳转:
      keyword: "",
      result: [],
      timeout: null,
    };
  },

  methods: {
    init() {
      //从路由中获取参数
      var keyWord = this.$route.query.keyword;
      //赋值参数
      this.searchParams.keyword = keyWord;
      this.keyword = this.keyword;

      searchGoodDetail(this.searchParams).then((Response) => {
        this.goodList = Response.data.goodList;

        this.total = Response.data.total;
      });
    },
    //当前页面数变化时
    handleCurrentChange(val) {
      //发送请求
      this.searchParams.currentPage = val;
      searchGoodDetail(this.searchParams).then((response) => {
        this.goodList = response.data.goodList;
        this.total = response.data.total;
      });
    },

    //当前页面大小变化时
    handleSizeChange(val) {
      this.searchParams.pageSize = val;
      searchGoodDetail(this.searchParams).then((response) => {
        this.goodList = response.data.goodList;
        this.total = response.data.total;
      });
    },
    //搜索单词
    search() {
      searchGoodDetail(this.searchParams).then((response) => {
        this.goodList = response.data.goodList;
        this.total = response.data.total;
      });
    },

    //跳转进行搜索
    searchkeyword() {
      this.$router
        .push({
          path: "/searchdetail",
          query: { keyword: this.keyword },
        })
        .catch((reason) => {
          window.location.reload();
        });
    },

    //跳转商品页面
    gotoGoodDetail(goodId) {
      alert(goodId);
      this.$router.push({ path: "/GoodDetail", query: { goodId: goodId } });
    },

    querySearchAsync(queryString, cb) {
      //清空搜索建议
      this.result = [];

      if (queryString === "") {
        cb(this.result);
        return;
      }

      //发送搜索请求
      searchGood(queryString).then((Response) => {
        this.result = Response.data;
        cb(this.result);
      });
    },

    handleSelect(item) {
      console.log(item.value);
      //跳转搜索页面
      this.$router.push({
        path: "/searchdetail",
        query: { keyword: item.value },
      });
    },
  },

  created() {
    this.init();
  },
};
</script>

<style>
.card {
  width: 380px;
  height: 300px;
  float: left;
  margin-left: 20px;
  margin-bottom: 20px;
}

.hotgood {
  width: 100%;
  margin-bottom: 20px;
}

.goodImg {
  height: 150px;
  width: 380px;
}

/* 清除浮动 */
.clearfix:after {
  content: "020";
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}

.clearfix {
  /* 触发 hasLayout */
  zoom: 1;
}
</style>